package org.csu.mypetstore.domain;

import java.math.BigDecimal;
import org.csu.mypetstore.vo.ItemVO;

public class CartItem {

    private String itemId;
    private String userId;
    private Item item;
    private int quantity;
    private boolean inStock;
    private BigDecimal total = BigDecimal.ZERO; // 初始化避免null

    // Getter/Setter for userId
    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    // Getter/Setter for itemId
    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    // Getter/Setter for item
    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
        calculateTotal();
    }

    // Getter/Setter for quantity
    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        if (quantity > 0) { // 防止负数或零
            this.quantity = quantity;
            calculateTotal();
        }
    }

    // Getter/Setter for inStock
    public boolean isInStock() {
        return inStock;
    }

    public void setInStock(boolean inStock) {
        this.inStock = inStock;
    }

    // Getter for total
    public BigDecimal getTotal() {
        return total;
    }

    // 增加数量
    public void incrementQuantity() {
        setQuantity(quantity + 1); // 复用校验逻辑
    }


    // 计算总价
    public void calculateTotal() {
        if (item != null && item.getListPrice() != null && quantity > 0) {
            total = item.getListPrice().multiply(BigDecimal.valueOf(quantity));
        } else {
            total = BigDecimal.ZERO; // 避免返回null
        }
    }
}


